package com.hazy.animalface.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hazy.animalface.R;
import com.hazy.animalface.module.StickerBag;

import java.util.List;

/**
 * Created by pengjiahui on 2017/4/15 0015.
 * <p>
 * 服务器头贴的适配器
 */
public class DownloadStickersAdapter extends RecyclerView.Adapter<DownloadStickersAdapter.MyViewHolder> {

    private Context context;
    private List<StickerBag> stickerBags;
    private ILoadItemStickerDetail iLoadItemStickerDetail;

    public DownloadStickersAdapter(Context context, List<StickerBag> stickerBags, ILoadItemStickerDetail iLoadItemStickerDetail) {
        this.context = context;
        this.stickerBags = stickerBags;
        this.iLoadItemStickerDetail = iLoadItemStickerDetail;
    }

    @Override
    public DownloadStickersAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_sticker_server, parent, false));
    }

    @Override
    public void onBindViewHolder(final DownloadStickersAdapter.MyViewHolder holder, int position) {
        final StickerBag stickerBag = stickerBags.get(position);
        if (stickerBag != null) {
            holder.mThumbImageView.setImageURI(stickerBag.getThumb().getThumbnailUrl(true, 200, 200));
            if (stickerBag.getPrice() != null) {
                Double price = Double.valueOf(stickerBag.getPrice());
                if (price > 0) {
                    holder.mPriceTextView.setText("￥ " + stickerBag.getPrice());
                } else {
                    holder.mPriceTextView.setText("免 费");
                }
            } else {
                holder.mPriceTextView.setText("免 费");
            }

            // 将头贴点击处理回调给Activity处理
            holder.mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    iLoadItemStickerDetail.onLoadDetail(stickerBag);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return stickerBags == null ? 0 : stickerBags.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        View mItemView;
        SimpleDraweeView mThumbImageView;
        Button mPriceTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            mThumbImageView = (SimpleDraweeView) itemView.findViewById(R.id.item_sticker_iv);
            mPriceTextView = (Button) itemView.findViewById(R.id.item_download_stickers_price_btn);
        }
    }

    public interface ILoadItemStickerDetail {
        void onLoadDetail(StickerBag stickerBag);
    }
}
